package com.parse.ip.parseip.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.parse.ip.parseip.entity.City;
import com.parse.ip.parseip.entity.Telephone;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 传入IP地址解析url
 */
public class Parse {
    //传入IP地址,请求三方数据,解析IP归属地
    public static String parseUrl(String ip) throws IOException {
        City city = new City();
        Map<String,Object> map = new HashMap<String, Object>();
        Document document = Jsoup.connect("http://www.ip138.com/ips138.asp?ip="+ip+"&action=2")
                .timeout(5000).get();
        Element element = document.select(".ul1 li").first();
        String text = element.text();//本站数据：山东省淄博市  联通
        String network = text.substring(text.indexOf("：")+1);
        city.setIP(ip);
        city.setNetwork(network);
        map.put(ip,city);
        String string = JSON.toJSONString(map);
        System.out.println(string);
        return string;
    }

    public static Telephone parseTele(String tele) throws MalformedURLException {
       /* {
            "response": {
                "15624496620": {
                    "detail": {
                        "area": [{
                            "city": "淄博"
                        }],
                        "province": "山东",
                        "type": "domestic",
                        "operator": "联通"
                    },
                    "location": "山东淄博联通"
                }
            },
            "responseHeader": {
                "status": 200,
                "time": 1543200395144,
                "version": "1.1.0"
            }
        }*/
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet get = new HttpGet("http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel="+tele);
        String json = "";
        Telephone telephone = new Telephone();
        try {

            HttpResponse response = httpClient.execute(get);
            json = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSON.parseObject(json);
            JSONObject resObject = jsonObject.getJSONObject("response");
            JSONObject teleObject = resObject.getJSONObject(tele);
            JSONObject detailObject = teleObject.getJSONObject("detail");
            System.out.println(detailObject.toJSONString());
            JSONArray jsonArray = detailObject.getJSONArray("area");
            Iterator<Object> iterator = jsonArray.iterator();

            while (iterator.hasNext()){
                JSONObject cityObject = (JSONObject) iterator.next();
                telephone.setCity(cityObject.getString("city"));
            }

            telephone.setTelephone(tele);
            telephone.setCarrier((String) teleObject.get("location"));
            telephone.setCatName(detailObject.getString("operator"));
            telephone.setProvince(detailObject.getString("province"));


            System.out.println(json);
        }catch (Exception e){
            e.printStackTrace();
        }
        return telephone;
    }
    public static void main(String[] args) throws IOException {
        Telephone telephone = Parse.parseTele("15624496620");
        System.out.println(telephone);
    }
}
